var key = sessionStorage.getItem("key");
var districtid = sessionStorage.getItem("districtid");
$(document).ready(function(){
	district_Arr=JSON.parse(sessionStorage.getItem('district_Arr'));
	var Region_str='<option value="">-- Please Select Region --</option>';
	var District_str='<option value="">-- Please Select District -- </option>';
	console.log(district_Arr);
	var selected='';
	for(var i=0;i<district_Arr.length;i++){
		var district=district_Arr[i].district;
		selected="";
		for(var j=0;j<district.length;j++){
			if(district[j].id == districtid){
				selected='selected="selected"';
				District_str+='<option value="'+district[j].id+'" selected="selected">'+district[j].name+'</option>';
			}else{
				District_str+='<option value="'+district[j].id+'">'+district[j].name+'</option>';
			}
		}

		Region_str+='<option value="'+district_Arr[i].id+'" '+selected+'>'+district_Arr[i].name+'</option>';
		$('#District').html(District_str);
	}
	$('#Region').html(Region_str);
})

$('#Region').change(function () {
	var Region_id=$(this).val();
	if (Region_id==""){
		$('#District').html('<option value="">-- Please Select District -- </option>');
	}
	var District_str='<option value="">-- Please Select District -- </option>';
	for(var i=0;i<district_Arr.length;i++){
		if (district_Arr[i].id == Region_id){
			var district=district_Arr[i].district;
			for(var j=0;j<district.length;j++){
				District_str+='<option value="'+district[j].id+'">'+district[j].name+'</option>';
			}
			$('#District').html(District_str);
			break;
		}
	}
})

//$('#Region').change(function () {
//	var Region_id=$(this).val();
//	if (Region_id==""){
//		$('#District').html('<option value="">-- Please Select District -- </option>');
//	}
//	var District_str='<option value="">-- Please Select District -- </option>';
//	for(var i=0;i<district_Arr.length;i++){
//		if (district_Arr[i].id == Region_id){
//			var district=district_Arr[i].district;
//			for(var j=0;j<district.length;j++){
//				District_str+='<option value="'+district[j].id+'">'+district[j].name+'</option>';
//			}
//			$('#District').html(District_str);
//			break;
//		}
//	}
//})



$(function(){
	init()
})
function init(){
	var addressName = sessionStorage.getItem("addressName");
	var defaul = sessionStorage.getItem("defaul");
	var address = sessionStorage.getItem("address").split(",");
	$('#nikeName').val(addressName);

	$('#floor').val(address[0]);
	$('#building').val(address[1]);
	$('#estate').val(address[2]);
	$('#street').val(address[3]);
	if( defaul == "Y"){
		$('.checkBox').addClass('on');
	}else if( defaul == "N"){
		$('.checkBox').removeClass('on');
	}
}

function save(){
	var defaul = 'N';
	if ( $('.checkBox').hasClass('on')) {
		defaul = 'Y';
	}else {
		defaul = 'N';
	}
	var district = $('#District option:selected').val();
	var arr = {
		hf_s_id: 'WD11',
		hf_srv_id: 'Cv_jcus_address',
		hs_action_id: 'update',
		hs_addr_key:key,
		hs_addr_name: $('#nikeName').val(),
		hs_addr_1: $('#floor').val(),
		hs_addr_2: $('#building').val(),
		hs_addr_3: $('#estate').val(),
		hs_addr_4: $('#street').val(),
		hs_def_addr_ind:defaul,
		hs_district_id:	district
	}
	getAjaxRequest(arr,function(result){
		list();
		window.history.go(-1)
	})
}
$('.btnBox a.mui-btn').on('tap',function(){
	save();
})
/*
$('.btnBox a.mui-btn').on('tap',function(){
	var userAdd={};
	userAdd.name=$('#name').val();
	userAdd.address=$('#nikeName').val();
	userAdd.phone=$('#phone').val();
	sessionStorage.setItem('userAdd',JSON.stringify(userAdd));
	location.href=MPJ.base+"/pages/order/confirm-detail.jsp";
})
*/


$('.checkBox').on('tap',function(){
	if($(this).hasClass('on')){
		$('.checkBox').removeClass('on');
	}else{
		$('.checkBox').addClass('on');
	}
})
